package cn.qzzg.controller;

import cn.qzzg.common.ResultBean;
import cn.qzzg.dto.PagingQueryDTO;
import cn.qzzg.enums.CodeEnum;
import cn.qzzg.pojo.PotTime;
import cn.qzzg.pojo.Review;
import cn.qzzg.pojo.TestTube;
import cn.qzzg.service.PotTimeService;
import cn.qzzg.service.ReviewService;
import cn.qzzg.service.TestTubeService;
import cn.qzzg.vo.ReviewVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/review")
public class ReviewController {
    @Resource
    ReviewService reviewService;
    @Resource
    TestTubeService testTubeService;

    @Resource
    PotTimeService potTimeService;

    /**
     * 查询复查的试管码信息 (新增新锅次编码，新锅次位置)
     * @param pagingQueryDTO
     * @return
     */
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    public ResultBean<?> queryPage(@RequestBody PagingQueryDTO<Review> pagingQueryDTO) {
        Page<Review> page1 = reviewService.findPage(pagingQueryDTO);
        List<Review> reviewList = page1.getRecords();
        List<ReviewVO> reviewVOList = new ArrayList<>();
        reviewList.forEach((review) -> {
            TestTube testTube = testTubeService.findById(review.getTestTubeId());

            Integer newPotTimeId = testTube.getNewPotTimeId();
            Date dates=null;
            if(newPotTimeId!=null){
                PotTime potTimeInfo = potTimeService.findPotTimeInfo(testTube.getNewPotTimeId());
                dates = potTimeInfo.getCreateTime();
            }

            ReviewVO reviewVO = new ReviewVO();
            reviewVO.setTestTubeId(testTube.getTestTubeId())
                    .setReviewType(review.getReviewType())
                    .setReagentType(review.getReagentType())
                    .setOGene(testTube.getOGene())
                    .setNGene(testTube.getNGene())
                    .setPotTimeId(testTube.getPotTimeId())
                    .setPotTimeAddress(testTube.getPotTimeAddress())
                     .setNewPotTimeId(testTube.getNewPotTimeId())
                    .setNewPotTimeAddress(testTube.getNewPotTimeAddress())
                    .setCreateTime(getTime(dates))
                    .setTestResults(testTube.getTestResults());
            reviewVOList.add(reviewVO);
        });
        Page<ReviewVO> resultsPage = new Page<>();
        resultsPage.setTotal(page1.getTotal()).setRecords(reviewVOList);

        return ResultBean.success(resultsPage);
    }
    static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static String getTime(Date date){
        if(date!=null){
            return dateFormat.format(date);
        }
        return "";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResultBean<?> create(@RequestBody ReviewVO reviewVO) {
        List<TestTube> testTubeList = testTubeService.findAllByPotTimeId(reviewVO.getPotTimeId().toString());
        if (reviewService.check(reviewVO.getTestTubeId()))
            return ResultBean.error(CodeEnum.CUSTOM_ERROR_F0101);
        for (TestTube testTube : testTubeList) {
            if (testTube.getTestTubeId().equals(reviewVO.getTestTubeId())) {
                Review review = new Review();
                review.setTestTubeId(reviewVO.getTestTubeId())
                        .setReviewType(reviewVO.getReviewType())
                        .setReagentType(reviewVO.getReagentType())
                        .setPotTimeId(reviewVO.getPotTimeId());
                testTube.setTestResults(3);
                testTubeService.updateOne(testTube);
                return ResultBean.success(reviewService.saveOne(review));
            }
        }
        return ResultBean.error(CodeEnum.CUSTOM_ERROR_F0102);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultBean<?> update(@RequestBody List<ReviewVO> reviewSelectedList) {
        ReviewVO reviewForm = reviewSelectedList.get(reviewSelectedList.size() - 1);
        reviewSelectedList.remove(reviewSelectedList.size() - 1);
        reviewSelectedList.forEach(item -> {
            Review review = reviewService.findByTestTubeId(item.getTestTubeId());
            TestTube testTube = testTubeService.findById(item.getTestTubeId());
            if (reviewForm.getTestResults() != null)
                testTube.setTestResults(reviewForm.getTestResults());
            if (reviewForm.getReviewType() != null)
                review.setReviewType(reviewForm.getReviewType());
            if (reviewForm.getReagentType() != null)
                review.setReagentType(reviewForm.getReagentType());
            if (reviewForm.getOGene() != null)
                testTube.setOGene(reviewForm.getOGene());
            if (reviewForm.getNGene() != null)
                testTube.setNGene(reviewForm.getNGene());
            testTubeService.updateOne(testTube);
            reviewService.updateOne(review);
        });
        return ResultBean.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultBean<?> delete(@RequestBody List<ReviewVO> reviewSelectedList) {
        reviewSelectedList.forEach(reviewVO -> {
            TestTube testTube = testTubeService.findById(reviewVO.getTestTubeId());
            if (testTube.getTestResults() == 3) {
                testTube.setTestResults(0);
                testTubeService.updateOne(testTube);
            }
            reviewService.removeOne(reviewVO.getTestTubeId());
        });
        return ResultBean.success();
    }
}
